#include "ipconfig.h"
#include "ui_ipconfig.h"
#include "msgbox.h"
#include "commonfunction.h"
#include "constants.h"
#include "systempreset.h"
#include "userlogger.h"

IPConfig::IPConfig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::IPConfig)
{
    bdf = BllDataFlow::getInstance();
    connect(bdf, &BllDataFlow::retranslateUiSignal, this, &IPConfig::retranslateUiSlot);
    ui->setupUi(this);
    initWidget();
}

IPConfig::~IPConfig()
{
    delete ui;
}

void IPConfig::on_btnLocalNet_clicked()
{
    QString ipaddr = ui->ipaddr->getIP();
    QString subnetMask = ui->subnetMask->getIP();
    QString defaultGateway = ui->defaultGateway->getIP();
    QString dnsAddr = ui->DNS->getIP();
    if(!checkIPValid(ipaddr) ||
       !checkIPValid(subnetMask)||
       !checkIPValid(defaultGateway)||
       !checkIPValid(dnsAddr))
    {
        MsgBox::warning(this,QString(tr("Ethernet")),QString(tr("Local ethernet input invalid!")));
        return;
    }

    QString message;
    if (this->ipaddr != ipaddr)
    {
        message += QString("%1: %2 -> %3;").arg(tr("IP address")).arg(this->ipaddr).arg(ipaddr);
        this->ipaddr = ipaddr;
    }

    if (this->subnetMask != subnetMask)
    {
        message += QString("%1: %2 -> %3;").arg(tr("Subnet mask")).arg(this->subnetMask).arg(subnetMask);
        this->subnetMask = subnetMask;
    }

    if (this->defaultGateway != defaultGateway)
    {
        message += QString("%1: %2 -> %3;").arg(tr("Gateway")).arg(this->defaultGateway).arg(defaultGateway);
        this->defaultGateway = defaultGateway;
    }

    if (this->dnsAddr != dnsAddr)
    {
        message += QString("%1: %2 -> %3;").arg(tr("DNS server")).arg(this->dnsAddr).arg(dnsAddr);
        this->dnsAddr = dnsAddr;
    }

    if (message.length() > 0)
    {
        UserLogger::log(message);
        saveLocalParam();

        MsgBox msgbox;
        msgbox.setTitle(QString(tr("Ethernet")));
        msgbox.setText(QString(tr("Local internet config OK!")));
        msgbox.setBtnCancelVisible(false);
        msgbox.exec();
    }
}

void IPConfig::on_btnMaterialNet_clicked()
{
    QString remoteIP = ui->remoteIP->getIP();
    if(!checkIPValid(remoteIP))
    {
         MsgBox::warning(this,QString(tr("Ethernet")),QString(tr("Remote IP input invalid!")));
         return;
    }

    if (this->remoteIP == remoteIP)
    {
        return;
    }

    UserLogger::log(QString("%1: %2 -> %3").arg(tr("Remote IP")).arg(this->remoteIP).arg(remoteIP));
    this->remoteIP = remoteIP;
    saveRemoteIPParam();

    MsgBox msgbox;
    msgbox.setTitle(QString(tr("Ethernet")));
    msgbox.setText(QString(tr("Material internet config OK!")));
    msgbox.setBtnCancelVisible(false);
    msgbox.exec();
}

void IPConfig::on_btnPrintNet_clicked()
{
    //todo: save parameters
    QString printIP = ui->printIP->getIP();
    if(!checkIPValid(printIP))
    {
         MsgBox::warning(this,QString(tr("Ethernet")),QString(tr("Print IP input invalid!")));
         return;
    }

    if (this->printIP == printIP)
    {
        return;
    }

    UserLogger::log(QString("%1: %2 -> %3").arg(tr("Printer IP")).arg(this->printIP).arg(printIP));
    this->printIP = printIP;
    savePrintIPParam();

    MsgBox msgbox;
    msgbox.setTitle(QString(tr("Ethernet")));
    msgbox.setText(QString(tr("Print internet config OK!")));
    msgbox.setBtnCancelVisible(false);
    msgbox.exec();
}

void IPConfig::retranslateUiSlot()
{
    ui->retranslateUi(this);
}

void IPConfig::initWidget()
{
    setShadow({ui->subnetMask,ui->defaultGateway,ui->ipaddr,ui->DNS, ui->remoteIP, ui->printIP});
    readParam();
}

void IPConfig::readParam()
{
    ipaddr = SystemPreset::instance()->getStringData(EnumPresetItem::IPAddress);
    subnetMask = SystemPreset::instance()->getStringData(EnumPresetItem::SubnetMask);
    defaultGateway = SystemPreset::instance()->getStringData(EnumPresetItem::Gateway);
    dnsAddr = SystemPreset::instance()->getStringData(EnumPresetItem::DNS);
    remoteIP = SystemPreset::instance()->getStringData(EnumPresetItem::RemoteIP);
    printIP = SystemPreset::instance()->getStringData(EnumPresetItem::PrintIP);

    ui->ipaddr->setIP(ipaddr);
    ui->subnetMask->setIP(subnetMask);
    ui->defaultGateway->setIP(defaultGateway);
    ui->DNS->setIP(dnsAddr);
    ui->remoteIP->setIP(remoteIP);
    ui->printIP->setIP(printIP);
}

void IPConfig::saveLocalParam()
{
    SystemPreset::instance()->setData(EnumPresetItem::IPAddress, ipaddr);
    SystemPreset::instance()->setData(EnumPresetItem::SubnetMask, subnetMask);
    SystemPreset::instance()->setData(EnumPresetItem::Gateway, defaultGateway);
    SystemPreset::instance()->setData(EnumPresetItem::DNS, dnsAddr);
}

void IPConfig::saveRemoteIPParam()
{
    SystemPreset::instance()->setData(EnumPresetItem::RemoteIP, remoteIP);
}

void IPConfig::savePrintIPParam()
{
    SystemPreset::instance()->setData(EnumPresetItem::PrintIP, printIP);
}
